//制作跳伞AI
//制作跳伞AI行为
let All_Player = [
    "Player_1",
    "Player_2",
    "Player_3",
    "Player_4",
    "Player_5",
    "Player_6",
];
let All_Faction = ["Soviet", "Allied", "Japan", "Celestial"];
let PointCount = 20;
let PointText = "AISpawnPoint";

//删除ai的双刃与蛋蛋车
let index = 1;
All_Player.forEach((player) => {
    script
        .begin()
        .setName("Remove_AI_Aircraft_and_Vehicle" + player)
        .conditionNot("PLAYER_IS_HUMAN_OR_AI_PERSONALITY", player, "Human")
        .onTrue("NAMED_DELETE", "mcv_" + index)
        .onTrue("NAMED_DELETE", "hikouki_" + index)
        .ed("/" + player);
    index++;
});

//创建变量表示点是否被占用
let CreatePoint = script
    .begin()
    .setName("CreateVar")
    .condition("CONDITION_TRUE");
for (let i = 1; i <= PointCount; i++) {
    CreatePoint = CreatePoint.onTrue("SET_COUNTER", "PointPlaced" + i, 0);
}
All_Player.forEach((player) => {
    CreatePoint = CreatePoint.onTrue("SET_COUNTER", player + "Placed", 0);
});
All_Player.forEach((player) => {
    CreatePoint = CreatePoint.onTrue(
        "ENABLE_SCRIPT",
        "CreateRandomVar" + player
    );
});
CreatePoint.ed();

//创建各个玩家的随机数
index = 1;
All_Player.forEach((player) => {
    let temp = script
        .begin()
        .setName("CreateRandomVar" + player)
        .setRepeat()
        .setActive(false)
        .conditionNot("PLAYER_IS_HUMAN_OR_AI_PERSONALITY", player, "Human")
        .onTrue("SET_RANDOM_COUNTER", player + "RandomVar", 1, PointCount)
        .onFalse("NAMED_DELETE", "Base" + index);
    for (var i = 1; i <= PointCount; i++) {
        temp = temp.onTrue("ENABLE_SCRIPT", player + "CheckRand" + i);
    }
    temp.ed("/" + player);
    index++;
});

//检查各个玩家的随机数
index = 1;
All_Player.forEach((player) => {
    for (var i = 1; i <= PointCount; i++) {
        script
            .begin()
            .setName(player + "CheckRand" + i)
            .setActive(false)
            .condition("COUNTER", player + "RandomVar", 2, i)
            .condition("COUNTER", "PointPlaced" + i, 5, 1)
            .onTrue("SET_COUNTER", "PointPlaced" + i, 1)
            .onTrue("UNIT_TELEPORT_TO_WAYPOINT", "Base" + index, PointText + i)
            .onTrue("ENABLE_SCRIPT", player + "ReplaceMCV")
            .onTrue("DISABLE_SCRIPT", "CreateRandomVar" + player)
            .ed("/" + player);
    }
    index++;
});

//替换基地车
All_Player.forEach((player) => {
    let temp = script
        .begin()
        .setName(player + "ReplaceMCV")
        .setActive(false)
        .condition("TIMER_EXPIRED", "AI_begin");
    All_Faction.forEach((faction) => {
        temp = temp.onTrue("ENABLE_SCRIPT", player + "ReplaceMCV_" + faction);
    });
    temp.ed("/" + player);
});

//苏联基地车
index = 1;
All_Player.forEach((player) => {
    script
        .begin()
        .setName(player + "ReplaceMCV_Soviet")
        .setActive(false)
        .condition("PLAYER_HAS_PLAYER_TECH", player, "PlayerTech_Soviet")
        .onTrue(
            "UNIT_SPAWN_NAMED_OBJECT_ON_TEAM_AT_NAMED_OBJECT_LOCATION",
            player + "MCV",
            "sovietmcv",
            player + "/" + "team" + player,
            "Base" + index
        )
        .onTrue("NAMED_DELETE", "Base" + index)
        .onTrue("NAMED_DELETE", "d" + index)
        .onTrue("NAMED_TRANSFER_OWNERSHIP_PLAYER", player + "MCV", player)
        .ed("/" + player);
    index++;
});

//盟军基地车
index = 1;
All_Player.forEach((player) => {
    script
        .begin()
        .setName(player + "ReplaceMCV_Allied")
        .setActive(false)
        .condition("PLAYER_HAS_PLAYER_TECH", player, "PlayerTech_Allied")
        .onTrue(
            "UNIT_SPAWN_NAMED_OBJECT_ON_TEAM_AT_NAMED_OBJECT_LOCATION",
            player + "MCV",
            "alliedmcv",
            player + "/" + "team" + player,
            "Base" + index
        )
        .onTrue("NAMED_DELETE", "Base" + index)
        .onTrue("NAMED_TRANSFER_OWNERSHIP_PLAYER", player + "MCV", player)
        .onTrue("NAMED_DELETE", "d" + index)
        .ed("/" + player);
    index++;
});

//帝国基地车
index = 1;
All_Player.forEach((player) => {
    script
        .begin()
        .setName(player + "ReplaceMCV_Japan")
        .setActive(false)
        .condition("PLAYER_HAS_PLAYER_TECH", player, "PlayerTech_Japan")
        .onTrue(
            "UNIT_SPAWN_NAMED_OBJECT_ON_TEAM_AT_NAMED_OBJECT_LOCATION",
            player + "MCV",
            "japanmcv",
            player + "/" + "team" + player,
            "Base" + index
        )
        .onTrue("NAMED_DELETE", "Base" + index)
        .onTrue("NAMED_TRANSFER_OWNERSHIP_PLAYER", player + "MCV", player)
        .onTrue("NAMED_DELETE", "d" + index)
        .ed("/" + player);
    index++;
});

//神州龙船
index = 1;
All_Player.forEach((player) => {
    script
        .begin()
        .setName(player + "ReplaceMCV_Celestial")
        .setActive(false)
        .conditionNot("PLAYER_HAS_PLAYER_TECH", player, "PlayerTech_Soviet")
        .conditionNot("PLAYER_HAS_PLAYER_TECH", player, "PlayerTech_Allied")
        .conditionNot("PLAYER_HAS_PLAYER_TECH", player, "PlayerTech_Japan")
        .onTrue(
            "UNIT_SPAWN_NAMED_OBJECT_ON_TEAM_AT_NAMED_OBJECT_LOCATION",
            player + "MCV",
            "celestialmcv",
            player + "/" + "team" + player,
            "Base" + index
        )
        .onTrue("NAMED_DELETE", "Base" + index)
        .onTrue("NAMED_TRANSFER_OWNERSHIP_PLAYER", player + "MCV", player)
        .onTrue("NAMED_DELETE", "d" + index)
        .ed("/" + player);
    index++;
});
